package com.heima.test93;

public class Test09 {

    public static void main(String[] args) {
        //demo1();
       /* demo2();
        demo3();
        demo4();
        demo5();*/

        String s1 = "abc";
        System.out.println(s1==("a"+"bc"));

        String s2 = "a";
        String s3 = s2+"bc";
        System.out.println(s1==s3);
    }

    private static void demo5() {
        String s1 = "ab";
        String s2 = "abc";
        String s3 = s1 + "c";
        System.out.println(s3 == s2);     // false
        System.out.println(s3.equals(s2));//true
    }

    private static void demo4() {
        // byte b = 3 + 4;
        String s1 = "a" + "b" + "c";    // 如果字符串相加 ：常量优化机制
        String s2 = "abc";
        System.out.println(s1 == s2);   // true
        System.out.println(s1.equals(s2));//true
    }

    private static void demo3() {// ==比较的是地址值
        String s1 = new String("abc");
        String s2 = "abc";
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }

    private static void demo2() {
        // 创建几个对象
        // 创建两个对象,一个在常量池中,一个在堆内存中
        String s1 = new String("abc");  // 两个
        System.out.println(s1);
    }

    private static void demo1() {
        String s1 = "abc";
        String s2 = "abc";
        System.out.println(s1 == s2);    //true
        System.out.println(s1.equals(s2)); // true
    }
}
